import { Component, OnInit } from '@angular/core';

// get  请求需要引入 HttpClient 
// post 请求还需要引入 HttpHeaders
import { HttpClient,HttpHeaders} from '@angular/common/http'

@Component({
  selector: 'app-post',
  templateUrl: './post.component.html',
  styleUrls: ['./post.component.css']
})
export class PostComponent implements OnInit {
  public list :any[]=[]
  constructor(public http:HttpClient) { }
  
  public name:any="hellpo"

  ngOnInit() {
  }

  getData(){
    // alert('执行get请求')
    // 服务器必须允许跨域
    let api="http://192.168.31.16/chfs/files?filepath=/%E6%96%B0%E5%BB%BA%E6%96%87%E4%BB%B6%E5%A4%B9&_=1572860427944"
    // rxjs
    this.http.get(api).subscribe((result:any[])=>{
      console.log(result);
      this.list=result;
      
    })
  }

  dologin(){
    // alert("这里是dologin")
    // 固定的写法，记不住复制
    const httpOptions={headers :new HttpHeaders({  'Content-Type': 'application/json'})}
    // 存在跨域
    let api="http://192.168.31.16/chfs/session"
    this.http.post(api,{"user":"hushan","pwd":"123"},httpOptions).subscribe((response:any)=>{
      console.log(response)
      
    })
  }

  getJsonp(){
    // jsonp 请求，服务器必须支持 jsonp
    let api="http://hs.freeforf.com:7802/files"
    this.http.jsonp(api,'callback').subscribe((result)=>{
      console.log(result)
    })
  }
}
